#ifndef __HTTP_WEB_REQUEST_H__
#define __HTTP_WEB_REQUEST_H__

#define HTTP_WEB_REQUEST_POST_FORMAT "%s %s HTTP/1.1\r\n\
Host: %s\r\n\
Connection: keep-alive\r\n\
Content-Length: %s\r\n\
Origin: %s\r\n\
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11\r\n\
Content-Type: application/x-www-form-urlencoded\r\n\
Accept: */*\r\n\
Accept-Encoding: gzip,deflate,sdch\r\n\
Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4\r\n\
Accept-Charset: Big5,utf-8;q=0.7,*;q=0.3\r\n\
%s\
\r\n\
%s"

class HTTPWebRequest
{
public:
	// ctor
	HTTPWebRequest( const char* addr, const unsigned short port, const char* path, const char* method="POST" );
	// dtor
	virtual ~HTTPWebRequest();
	
	// set member vars
	// addr can be ip address or hostname
	int SetAddr( const char* addr );
	int SetPort( const unsigned short port );
	// path with the first char is "/"
	int SetPath( const char* path );
	// set method
	int SetMethod( const char* method );
	
	// set parameters
	int SetParameter( std::string key, std::string value );
	int ClearParameters();
	
	// set header
	int SetHeader( std::string key, std::string value );
	int ClearHeaders();

	// connect to server to request
	int Request();
	
	// get response
	std::string GetResponse();

protected:
	// clear garbage
	virtual void FreeBuffers();
	
	// set sock address for connect
	int SetSockAddrIn( struct sockaddr_in* p_addr_struct );
	
	// do connect
	virtual int DoConnect();
	
	// prepare the request to send to server
	int PrepareRequestStr();
	
	// send & recv
	virtual int DoSend();
	virtual int DoRecv();




	// server info
	std::string m_server_addr;
	unsigned short m_server_port;
	std::string m_server_path;
	
	// request info
	std:: string m_method;
	std::map<std::string, std:: string> m_headers;
	
	// parameters first=second
	std::map<std::string, std::string> m_parameters;
	
	// socket for connection
	int m_sock;

	// request str
	int m_request_str_len;
	char* m_request_str;

	// response str
	std::string m_response_str;
};

#if 0
// do query
POST /mops/web/ajax_t51sb08 HTTP/1.1
Host: mops.twse.com.tw
Connection: keep-alive
Content-Length: 72
Origin: http://mops.twse.com.tw
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: Big5,utf-8;q=0.7,*;q=0.3
Cookie: jcsession=jHttpSession@9d75c5; newmops2=selfObj%3DtagCon1%7CTYPEK%3Dsii%7Cyear%3D100%7Cseason%3D02%7C

encodeURIComponent=1&step=1&firstin=1&off=1&TYPEK=sii&year=100&season=02


// download csv
POST /server-java/t105sb02 HTTP/1.1
Host: mops.twse.com.tw
Connection: keep-alive
Content-Length: 62
Cache-Control: max-age=0
Origin: http://mops.twse.com.tw
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://mops.twse.com.tw/mops/web/t51sb08
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: Big5,utf-8;q=0.7,*;q=0.3
Cookie: newmops2=selfObj%3DtagCon1%7CTYPEK%3Dsii%7Cyear%3D100%7Cseason%3D02%7C

firstin=true&step=10&filename=t51sb08_5_20120313_172943938.csv

#endif

#endif
